<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="游戏名" prop="gameName">
        <el-input v-model="queryParams.gameName" placeholder="请输入游戏名" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="排序" prop="sort">
        <el-input v-model="queryParams.sort" placeholder="请输入排序" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['system:category:add']">新增</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="categoryList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="游戏类型表" align="center" prop="id" />
      <el-table-column label="排序" align="center" prop="sort" />
      <el-table-column label="图标" align="center">
        <template slot-scope="scope">
          <div class="full-size-image rounded-square">
            <el-image :src="scope.row.images" style="width: 100%; height: auto;">
              <div slot="placeholder" class="image-slot">
                <span class="dot">...</span>
              </div>
            </el-image>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="客户端" align="center" prop="type">
        <template slot-scope="scope">
          <span class="client-type" v-bind:class="{ 'green-bg': scope.row.type == 1, 'yellow-bg': scope.row.type == 2 }">
            {{ scope.row.type == 1 ? '端游' : '手游' }}
          </span>
        </template>
      </el-table-column>
      <el-table-column label="游戏名" align="center" prop="gameName">
        <template slot-scope="scope">
          <div class="custom-game-name-cell">
            {{ scope.row.gameName }}
          </div>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleEdit(scope.row)">属性编辑</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改【请填写功能名称】对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="700px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="游戏名" prop="gameName">
          <el-input v-model="form.gameName" placeholder="请输入游戏名" />
        </el-form-item>
        <el-form-item label="排序" prop="sort">
          <el-input v-model="form.sort" placeholder="请输入热度排序" />
        </el-form-item>
        <el-form-item label="图标">
          <el-row>
            <el-col>
              <imageUploadMore ref="pic_single" :isShowTip="false" :limit="1" :value="form.imgs"></imageUploadMore>
            </el-col>
          </el-row>
        </el-form-item>
        <el-form-item label="游戏客户端">
          <el-select v-model="form.type" placeholder="请选择">
            <el-option label="端游" value="1" />
            <el-option label="手游" value="2" />
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <addAttribute ref="attribute" :editing-data="currentEditing" :visible.sync="editVisible" />
  </div>
</template>

<script>
import { listCategory, getCategory, delCategory, addCategory, updateCategory, updaType } from "@/api/gmcategory/category";
import env from "@/utils/config.js";
import imageUploadMore from '@/components/ImageUpload/imageUploadMore.vue';
import { addAttribute } from "../../api/attribute/attribute";

export default {
  name: "Category",
  components: {
    imageUploadMore,
    addAttribute
  },
  data() {
    return {
      currentEditing: {}, // 当前编辑的数据
      editVisible: false, // 控制编辑组件的显示隐藏
      // 遮罩层
      loading: true,
      imgUrl: '',//上传图片地址
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 【请填写功能名称】表格数据
      categoryList: [],
      type: {},
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        gameName: null,
        sort: null,
        images: null,
        isItDisplayed: null,
        logicalDeletion: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        gameName: [
          { required: true, message: "游戏名不能为空", trigger: "blur" }
        ],
        sort: [
          { required: true, message: "热度排序不能为空", trigger: "blur" }
        ],
        logicalDeletion: [
          { required: true, message: "是否删除不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.imgUrl = env;
    this.getList();

  },
  methods: {
    getList() {
      this.loading = true;
      listCategory(this.queryParams).then(response => {
        this.categoryList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        gameName: null,
        sort: null,
        images: null,
        isItDisplayed: null,
        logicalDeletion: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "游戏配置";
    },
    handleEdit(row) {
      this.currentEditing = { ...row }; // 复制行数据避免直接修改引用
      this.editVisible = true; // 显示编辑组件
    },




    /** 提交按钮 */
    submitForm() {
      // 单张图片
      let imgs = this.$refs.pic_single.getImgArrByChild();
      // let imgs = this.$refs.pic_more.getImgArrByChild();

      if (imgs) {
        this.form.imgs = imgs;
        console.log("ssssssssssssssssss" + imgs);
      }
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateCategory(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCategory(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },

  }
};
</script>
<style lang="scss">
.client-type {
  display: inline-block;
  padding: 4px 8px;
  border-radius: 4px;
  font-weight: bold;
  transition: background-color 0.3s ease;
  /* 添加过渡效果，使颜色变化更平滑 */
}

.green-bg {
  background-color: #4291f8;
  /* 更柔和的绿色 */
  color: #ffffff;
}

.yellow-bg {
  background-color: #f1c40f;
  /* 柔和的黄色 */
  color: #ffffff;
  /* 黄色背景下使用深色文字 */
}

/* 可选：增加鼠标悬停效果 */
.client-type:hover {
  transform: scale(1.05);
  /* 稍微放大 */
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.2);
  /* 添加阴影 */
}

.custom-game-name-cell {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 6px 12px;
  /* 根据需要调整内边距 */
  border-radius: 4px;
  /* 圆角 */
  background-color: #858200;
  /* 背景颜色，可自行调整 */
  color: #ffffff;
  /* 文字颜色，可自行调整 */
  font-weight: bold;
  /* 加粗文字 */
  transition: background-color 0.3s ease;
  /* 过渡效果，可选 */
}

/* 可选：鼠标悬停效果 */
.custom-game-name-cell:hover {
  background-color: #2529f0;
  /* 悬停时的背景颜色，可自行调整 */
}



.full-size-image {
  width: 100%;
  position: relative;
  overflow: hidden;
}

.full-size-image>el-image {
  width: 100%;
  height: 100%;
  object-fit: cover;
  /* 保持图片比例，填充整个容器 */
  border-radius: 4px;
  /* 设置图片的圆角 */
  border: 1px solid #272626;
  /* 添加边框 */
}
</style>
